<?php
namespace app\admin666\controller;

use think\Controller;
use think\Request;
use think\Cache;
class Publics extends Controller
{
    /* 空操作，用于输出404页面 */
    public function _empty() {
        return '404';
    }
    
    public function _initialize(Request $request = null){
        define('NOW_TIME',time());
        $config =   cache('DB_CONFIG_DATA');
        if(!$config){
            $config =  config_lists();
            cache('DB_CONFIG_DATA',$config);
        }
        config($config); //添加配置
    }
    
    /**
	* 管理员登录
	* @date: 2016年11月21日 下午8:51:39
	* @author: CUI
	* @param: 
	* @return:
	*/
	public function login($username = null, $password = null, $passcode = null){		
	    if(Request::instance()->isPost()){
	        if(!captcha_check($passcode)){
				$this->error("验证码错误！");
			}

            /* $ip_status = $this->ipcheck();
            if ($ip_status==1){
                $this->error("你的IP为空，不允许登录，清联系管理员");
            }elseif ($ip_status==2){
                $this->error("你的IP不允许登录，清联系管理员");
            } */
			
			$model = model('Member');
			
			$uid = $model->login($username, $password);
			if($uid){
			    $this->success('登录成功！',url('Index/index'));	
			}else{
				$this->error($model->getError());	
			}	
		}else{
		    $res = $this->fetch();
		    return $res;
		}
    }
    
    /**
     * 退出登录
     * @date: 2016年11月21日 下午8:51:55
     * @author: CUI
     * @param: variable
     * @return:
     */
    public function logout(){
        model('Member')->logout();
        $this->success('退出成功！',url('publics/login'));
    }
    
    /**
    * 消除缓存
    * @date: 2018年6月6日 下午3:51:04
    * @author: xiaocui
    * @param: variable
    * @return:
    */
    public function clear_cache() {
        Cache::clear();
        $this->success('清除成功');
    }

    public function ipajax()
    {
        //获取IP,看IP是否允许登录
        $count = db("loginip")->count();
        if ($count==0){
            echo "0";
        }else{
            $request = Request::instance();
            $ip = $request->ip();
            if (empty($ip)){
                echo "1";
            }else{
                $count = db("loginip")->where("ip='$ip' and status=1")->count();
                if ($count==0){
                    echo "2";
                }else{
                    echo "0";
                }
            }
        }
    }

    public function ipcheck()
    {
        //获取IP,看IP是否允许登录
        $count = db("loginip")->count();
        if ($count==0){
            $status = "0";
        }else{
            $request = Request::instance();
            $ip = $request->ip();
            if (empty($ip)){
                $status = "1";
            }else{
                $count = db("loginip")->where("ip='$ip' and status=1")->count();
                if ($count==0){
                    $status = "2";
                }else{
                    $status = "0";
                }
            }
        }
        return $status;
    }
    
    public function test(){
        $list = db('column')->field('id,pid,title')->order('id asc')->select();
        import('util.Tree');
        $pid = \Tree::getParents($list,4,false);
        print_r($pid);
        
       // $list = db('column')->field('id,pid,title')->order('id asc')->select();
        //import('util.Tree');
        $pid = \Tree::getChilds($list,4,false);
        print_r($pid);
    }
}
